package org.sanguine.plugin.timetracker;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

import org.sanguine.plugin.timetracker.pojo.AccountInfo;
import org.sanguine.plugin.timetracker.providers.TrackerServiceProviderFactory;

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

/**
 * Created with IntelliJ IDEA. User: alex Date: 17.02.14 Time: 17:08 SVN $Id$
 */
public class TimerConfigForm {
    private JPanel rootPanel;
    private JTextField uriField;
    private JTextField usernameField;
    private JPasswordField passwordField;
    private JButton testButton;
    private JLabel userInfoLabel;
    private JComboBox provider;
    private final TimerComponent timerComponent;

    public TimerConfigForm(TimerComponent component) {
        this.timerComponent = component;

        testButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (timerComponent != null) {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            final AccountInfo info = timerComponent.checkCredential("https://" + uriField.getText(),
                                    usernameField.getText(), passwordField.getPassword());
                            EventQueue.invokeLater(new Runnable() {
                                @Override
                                public void run() {
                                    userInfoLabel.setText((info == null) ? "<html><p><b>Unauthorized</b></p></html>"
                                            : "<html><p>" + info.company + "</p>" + "<p>" + info.user + "</p></html>");
                                }
                            });
                        }
                    }).start();
                }
            }
        });

        provider.removeAllItems();
        for (String providerName : TrackerServiceProviderFactory.getProviders()) {
            provider.addItem(providerName);
        }
    }

    public void setData(TimerComponent data) {
        if (data.getUri() != null)
            uriField.setText(data.getUri().substring(8));
        else
            uriField.setText("");
        if (data.getUsername() != null)
            usernameField.setText(data.getUsername());
        else
            usernameField.setText("");
        if (data.getPassword() != null)
            passwordField.setText(new String(data.getPassword()));
        else
            passwordField.setText("");
        if (data.getProviderName() != null)
            provider.setSelectedItem(data.getProviderName());
        else
            provider.setSelectedIndex(0);
    }

    public void getData(TimerComponent data) {
        data.setUri("https://" + uriField.getText());
        data.setUsername(usernameField.getText());
        data.setPassword(passwordField.getPassword());
        data.setProviderName((String) provider.getSelectedItem());
    }

    public boolean isModified(TimerComponent data) {
        if (uriField.getText() != null ? !uriField.getText().equals(data.getUri().substring(8)) : data.getUri() != null)
            return true;
        if (usernameField.getText() != null ? !usernameField.getText().equals(data.getUsername())
                : data.getUsername() != null)
            return true;
        if (passwordField.getPassword() != null ? !passwordField.getPassword().equals(data.getPassword()) : data
                .getPassword() != null)
            return true;
        if (provider.getSelectedItem() != null ? !provider.getSelectedItem().equals(data.getProviderName()) : data
                .getProviderName() != null)
            return true;
        return false;
    }

    {
        // GUI initializer generated by IntelliJ IDEA GUI Designer
        // >>> IMPORTANT!! <<<
        // DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer >>> IMPORTANT!! <<< DO NOT edit this method OR call it in your
     * code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        rootPanel = new JPanel();
        rootPanel
                .setLayout(new FormLayout(
                        "fill:d:noGrow,left:4dlu:noGrow,fill:d:grow",
                        "center:d:noGrow,top:4dlu:noGrow,center:max(d;4px):noGrow,top:4dlu:noGrow,center:max(d;4px):noGrow,top:4dlu:noGrow,center:max(d;4px):noGrow,top:d:noGrow,top:205px:noGrow"));
        final JLabel label1 = new JLabel();
        label1.setFocusable(false);
        label1.setHorizontalAlignment(4);
        label1.setRequestFocusEnabled(false);
        label1.setText("Web Address https://");
        CellConstraints cc = new CellConstraints();
        rootPanel.add(label1, cc.xy(1, 1));
        uriField = new JTextField();
        uriField.setToolTipText("Harvest user web address");
        uriField.putClientProperty("html.disable", Boolean.TRUE);
        rootPanel.add(uriField, cc.xy(3, 1, CellConstraints.FILL, CellConstraints.DEFAULT));
        final JLabel label2 = new JLabel();
        label2.setFocusable(false);
        label2.setHorizontalAlignment(4);
        label2.setRequestFocusEnabled(false);
        label2.setText("Email");
        rootPanel.add(label2, cc.xy(1, 3));
        usernameField = new JTextField();
        usernameField.setToolTipText("Harvest username (e-mail)");
        usernameField.putClientProperty("html.disable", Boolean.TRUE);
        rootPanel.add(usernameField, cc.xy(3, 3, CellConstraints.FILL, CellConstraints.DEFAULT));
        final JLabel label3 = new JLabel();
        label3.setFocusable(false);
        label3.setHorizontalAlignment(4);
        label3.setRequestFocusEnabled(false);
        label3.setText("Password");
        rootPanel.add(label3, cc.xy(1, 5));
        passwordField = new JPasswordField();
        passwordField.setToolTipText("timetracker user password");
        passwordField.putClientProperty("html.disable", Boolean.TRUE);
        rootPanel.add(passwordField, cc.xy(3, 5, CellConstraints.FILL, CellConstraints.DEFAULT));
        testButton = new JButton();
        testButton.setMargin(new Insets(2, 0, 2, 0));
        testButton.setText("Check");
        testButton.setToolTipText("Check credential");
        rootPanel.add(testButton, cc.xy(3, 7));
        final JPanel panel1 = new JPanel();
        panel1.setLayout(new FormLayout("fill:d:grow", "center:d:grow"));
        rootPanel.add(panel1, cc.xy(3, 9, CellConstraints.DEFAULT, CellConstraints.FILL));
        panel1.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.black), "Info"));
        userInfoLabel = new JLabel();
        userInfoLabel.setFocusable(true);
        userInfoLabel.setMaximumSize(new Dimension(-1, -1));
        userInfoLabel.setMinimumSize(new Dimension(-1, -1));
        userInfoLabel.setPreferredSize(new Dimension(-1, -1));
        userInfoLabel.setRequestFocusEnabled(false);
        userInfoLabel.setText("");
        userInfoLabel.setVerticalAlignment(1);
        panel1.add(userInfoLabel, cc.xy(1, 1, CellConstraints.DEFAULT, CellConstraints.FILL));
        label1.setLabelFor(uriField);
        label2.setLabelFor(usernameField);
        label3.setLabelFor(passwordField);
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return rootPanel;
    }
}
